package model

import (
	"context"
	"demo/internal/errorx"
	"fmt"

	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ AnswerDetailModel = (*customAnswerDetailModel)(nil)

type (
	// AnswerDetailModel is an interface to be customized, add more methods here,
	// and implement the added methods in customAnswerDetailModel.
	AnswerDetailModel interface {
		answerDetailModel
		withSession(session sqlx.Session) AnswerDetailModel
		BulkInsert(context context.Context,session sqlx.Session,answerDetails []*AnswerDetail)error
	}

	customAnswerDetailModel struct {
		*defaultAnswerDetailModel
	}
)

// NewAnswerDetailModel returns a model for the database table.
func NewAnswerDetailModel(conn sqlx.SqlConn) AnswerDetailModel {
	return &customAnswerDetailModel{
		defaultAnswerDetailModel: newAnswerDetailModel(conn),
	}
}

func (m *customAnswerDetailModel) withSession(session sqlx.Session) AnswerDetailModel {
	return NewAnswerDetailModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customAnswerDetailModel) BulkInsert(context context.Context,session sqlx.Session,answerDetails []*AnswerDetail)error{
	conn := sqlx.NewSqlConnFromSession(session)
	query := fmt.Sprintf("insert into public.answer_detail (answer_id,question_id,ans_content) values (? ,? ,?)")
	bulk,err := sqlx.NewBulkInserter(conn,query)
	if err != nil {
		return errorx.NewBizError(500,"批量插入问题选项失败")
	}
	defer bulk.Flush()
	for _,val := range answerDetails {
		fmt.Println("插入的数据是：",val)
		bulk.Insert((*val).AnswerId,(*val).QuestionId,(*val).AnsContent)
	}
	return nil
}
